/**
 * @author <a href="mailto:wehrens@aei.mpg.de">Oliver Wehrens</a>
 * @version $Id$
 */

package org.gridsphere.provider.portletui.beans;

import java.util.ArrayList;
import java.util.List;

/**
 * The abstract <code>SelectElementBean</code> provides a base selectable element that is subclassed by
 * <code>RadioButtonBean</code>, <code>CheckBoxBean</code> and <code>ListBoxItemBean</code>
 */
public abstract class SelectElementBean extends BaseComponentBean implements TagBean {

    protected boolean selected = false;
    protected List<String> results = new ArrayList<String>();
    protected String onClick = null;


    public String getOnClick() {
        return onClick;
    }

    public void setOnClick(String onClick) {
        this.onClick = onClick;
    }

    /**
     * Constructs a default select element bean
     */
    public SelectElementBean() {
        super();
    }

    /**
     * Constructs a select element bean from a supplied visual bean type
     *
     * @param vbName the visual bean type
     */
    public SelectElementBean(String vbName) {
        super(vbName);
    }

    /**
     * Returns a String used in the final markup indicating if this bean is selected or not
     *
     * @param select the selected String
     * @return the String used in the final markup indicating if this bean is selected or not
     */
    protected String checkSelected(String select) {
        /*  @TODO@ MPR ADDED HERE
        if (!beanId.equals("") && value != null) {
            for (int ii = 0; ii < results.size(); ++ii) {
                String selectedValue = (String)results.get(ii);
                if (value.equals(selectedValue)) {
                    return " " + select + "='" + select + "' ";
                }
            }
            return "";
        }
        */
        if (selected) {
            return " " + select + " ";
        } else {
            return "";
        }
    }

    /**
     * Sets the selected status of the bean
     *
     * @param flag true if the bean is selected, false otherwise
     */
    public void setSelected(boolean flag) {
        this.selected = flag;
    }

    /**
     * Returns the selected status of the bean
     *
     * @return true if the bean is selected, false otherwise
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * Returns the selected values as a list
     *
     * @return selected values as a the list
     */
    public List<String> getSelectedValues() {
        return results;
    }

    /**
     * Returns the selected values as a list
     *
     * @return selected values as a the list
     */
    public String getSelectedValue() {
        if (results.isEmpty()) {
            return null;
        } else {
            return (String) results.get(0);
        }
    }

    /**
     * Adds a selected value to this bean
     *
     * @param value a selected value
     */
    public void addSelectedValue(String value) {
        results.add(value);
    }

    /**
     * Clears the selected values in this bean
     */
    public void clearSelectedValues() {
        selected = false;
        results.clear();
    }

    public String toStartString(String type) {
        String sname = createTagName(name);
        StringBuffer sb = new StringBuffer();
        sb.append("<input ");
        if (id != null) sb.append("id=").append(id).append(" ");
        sb.append(getFormattedCss());
        sb.append(" type='").append(type).append("' name='");
        if (value == null) value = "";
        sb.append(sname).append("' value='").append(value);
        sb.append("' ").append(checkDisabled()).append(" ");
        sb.append(checkSelected("checked=\"checked\""));

        if (onClick != null) sb.append(" onclick=\"").append(onClick).append("\"");
        sb.append(" />");
        return sb.toString();
    }

    public String toEndString() {
        return "";
    }
}
